package com.csun.dsms.service.impl;


import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.csun.dsms.comment.core.support.BaseService;
import com.csun.dsms.comment.core.utils.wrapper.WrapMapper;
import com.csun.dsms.comment.core.utils.wrapper.Wrapper;
import com.csun.dsms.mapper.HgFixedAssetDataMapper;
import com.csun.dsms.mapper.HgInvestmentDataMapper;
import com.csun.dsms.model.huang.HgFixedAssetData;
import com.csun.dsms.model.huang.HgInvestmentData;
import com.csun.dsms.model.huang.HgQueryDto;
import com.csun.dsms.model.huang.HgYearMonthVo;
import com.csun.dsms.service.HgFixedAssetDataService;
import com.csun.dsms.service.HgInvestmentDataService;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class HgInvestmentDataServiceImpl extends BaseService<HgInvestmentData>
        implements HgInvestmentDataService {

    @Resource
    private HgInvestmentDataMapper hgInvestmentDataMapper;
    
    @Override
    public List<HgInvestmentData> queryDataList(HgQueryDto hgQueryDto) {
        PageHelper.startPage(hgQueryDto.getPageNum(),hgQueryDto.getPageSize());
        return hgInvestmentDataMapper.queryListWithPage(hgQueryDto);
    }

    @Override
    public List<HgYearMonthVo> groupByMonth(HgQueryDto hgQueryDto) {
        return hgInvestmentDataMapper.groupByMonth(hgQueryDto);
    }

    @Override
    public Wrapper importInvestmentTableData(MultipartFile file) {
        try {
            ExcelReader reader = ExcelUtil.getReader(file.getInputStream());
            List<Map<String, Object>> ExcelList = reader.readAll();
            if (CollectionUtil.isEmpty(ExcelList)) {
                return WrapMapper.wrap(Wrapper.ERROR_CODE, "Excel不能为空！");
            }
            for (Map<String, Object> excelMap : ExcelList) {
                HgInvestmentData hgInvestmentData = new HgInvestmentData();
                hgInvestmentData.setStatisticalYear(Convert.toInt(excelMap.get("统计年份")));
                hgInvestmentData.setStatisticalMonth(Convert.toInt(excelMap.get("统计月")));
                hgInvestmentData.setArea(Convert.toStr(excelMap.get("数据的区域")));
                hgInvestmentData.setAreaLevel(Convert.toInt(excelMap.get("是市的还是区的")));
                hgInvestmentData.setAnnualTargetNum(Convert.toInt(excelMap.get("年度目标数")));
                hgInvestmentData.setCompletedNum(Convert.toInt(excelMap.get("完成个数")));
                hgInvestmentData.setCompletionRatio(Convert.toDouble(excelMap.get("完成比例")));
                hgInvestmentData.setYoyGrowth(Convert.toDouble(excelMap.get("同比增速")));
                hgInvestmentData.setAnnualTargetAmt(Convert.toDouble(excelMap.get("年度目标金额")));
                hgInvestmentData.setCompletedAmt(Convert.toDouble(excelMap.get("已完成金额")));
                hgInvestmentData.setCompletedAmtRatio(Convert.toDouble(excelMap.get("完成金额比例")));
                hgInvestmentData.setInvYoyGrowth(Convert.toDouble(excelMap.get("投资同比增速")));
                hgInvestmentData.setAnnualInvestedProjNum(Convert.toInt(excelMap.get("年度投资项目数量")));
                hgInvestmentData.setCompletedInvestedProjNum(Convert.toInt(excelMap.get("已完成投资项目数量")));
                hgInvestmentData.setCompletionInvestedProjRatio(Convert.toDouble(excelMap.get("完工投资项目比率")));
                hgInvestmentData.setInvestedProjYoyGrowth(Convert.toDouble(excelMap.get("投资项目同比增长")));
                hgInvestmentData.setAnnualStorageProjAmt(Convert.toDouble(excelMap.get("年度存储项目金额")));
                hgInvestmentData.setCompletedStorageProjAmt(Convert.toDouble(excelMap.get("已完成存储项目金额")));
                hgInvestmentData.setCompletedStorageProjAmtRatio(Convert.toDouble(excelMap.get("已完成存储项目金额比率")));
                hgInvestmentData.setStorageProjYoyGrowth(Convert.toDouble(excelMap.get("存储项目同比增长")));
                hgInvestmentData.setEntryDepartment(Convert.toStr(excelMap.get("上传的部门")));
                hgInvestmentData.setCreator(Convert.toStr(excelMap.get("上传人")));
                hgInvestmentData.setApproved(Convert.toInt(excelMap.get("是否审核")));
                hgInvestmentData.setApproval(Convert.toStr(excelMap.get("审核人")));
                hgInvestmentData.setCreatedTime(new Date());
                hgInvestmentDataMapper.insert(hgInvestmentData);
            }
            return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE, "导入数据成功！");
        } catch (Exception e) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, e.getMessage());
        }
    }
}
